package com.demo.steammanagement.common;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.demo.steammanagement.model.entity.*;
import com.demo.steammanagement.model.vo.*;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

public class QueryBuild {

    public static QueryWrapper<Games> gamesQueryWrapper(GamesVO vo) {
        QueryWrapper<Games> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .like(StringUtils.isNotBlank(vo.getName()), Games::getName, vo.getName())
                .like(vo.getId() != null, Games::getId, vo.getId())
                .orderByDesc(Games::getId)
                .eq(StringUtils.isNotBlank(vo.getCategory()), Games::getCategory, vo.getCategory());
        return queryWrapper;
    }

    public static QueryWrapper<GameAccounts> gamesAccountQueryWrapper(GameAccountVO vo) {
        QueryWrapper<GameAccounts> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .select(GameAccounts.class, info -> !info.getColumn().equals("stoken"))
                .like(StringUtils.isNotBlank(vo.getGameAccount()), GameAccounts::getGameAccount, vo.getGameAccount())
                .eq(ObjectUtils.isNotEmpty(vo.getGameId()), GameAccounts::getGameId, vo.getGameId())
                .eq(ObjectUtils.isNotEmpty(vo.getStatus()), GameAccounts::getStatus, vo.getStatus());
        return queryWrapper;
    }

    public static QueryWrapper<Announcements> announcementQueryWrapper(AnnouncementVO vo) {
        QueryWrapper<Announcements> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .orderByDesc(Announcements::getUpdatedAt)
                .like(StringUtils.isNotBlank(vo.getContent()), Announcements::getContent, vo.getContent())
                .between(ObjectUtils.isNotEmpty(vo.getBeginDate())&&ObjectUtils.isNotEmpty(vo.getEndDate()), Announcements::getCreatedAt, vo.getBeginDate(),vo.getEndDate());
        return queryWrapper;
    }

    public static QueryWrapper<Rentals> rentalsQueryWrapper(RentalVO vo) {
        QueryWrapper<Rentals> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .orderByDesc(Rentals::getCreatedAt)
                .eq(ObjectUtils.isNotEmpty(vo.getGameAccountId()), Rentals::getGameAccountId, vo.getGameAccountId())
                .eq(ObjectUtils.isNotEmpty(vo.getGameId()), Rentals::getGameId, vo.getGameId())
                .eq(ObjectUtils.isNotEmpty(vo.getStatus()), Rentals::getStatus, vo.getStatus())
                .eq(ObjectUtils.isNotEmpty(vo.getUserId()), Rentals::getUserId, vo.getUserId())
                .like(ObjectUtils.isNotEmpty(vo.getName()), Rentals::getName, vo.getName())
                .between(ObjectUtils.isNotEmpty(vo.getBeginDate())&&ObjectUtils.isNotEmpty(vo.getEndDate()), Rentals::getCreatedAt, vo.getBeginDate(),vo.getEndDate());
        return queryWrapper;
    }

    public static Wrapper<Users> usersQeuryWrapper(UserVO vo) {
        QueryWrapper<Users> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(ObjectUtils.isNotEmpty(vo.getAccount()), Users::getAccount, vo.getAccount())
                .between(ObjectUtils.isNotEmpty(vo.getBeginDate())&&ObjectUtils.isNotEmpty(vo.getEndDate()), Users::getCreatedAt, vo.getBeginDate(),vo.getEndDate());
        return queryWrapper;
    }
    
    public static QueryWrapper<RedeemCodeInfo> redeemCodeQueryWrapper(RedeemCodeInfoVO vo) {
        QueryWrapper<RedeemCodeInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .orderByDesc(RedeemCodeInfo::getId)
                .eq(StringUtils.isNotBlank(vo.getStatus()), RedeemCodeInfo::getStatus, vo.getStatus())
                .like(StringUtils.isNotBlank(vo.getRedeemCode()), RedeemCodeInfo::getRedeemCode, vo.getRedeemCode())
                .like(vo.getId() != null, RedeemCodeInfo::getId, vo.getId());
        return queryWrapper;
    }
}
